// pages/print/chooseSheet/chooseSheet.js
var utils = require("../../../utils/utils.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        serviceSubtypeId: 0,
        chooseIndex: '',
        list: []
    },
    //进行预览
    preViewSheet(e) {
        // wx.showLoading({
        //     title: '转换中',
        //     mask: true
        // })
        var that = this;
        that.selectComponent("#loading").showLoadingCus('转换中');
        var name = e.currentTarget.dataset.name;
        var url = e.currentTarget.dataset.url;
        var hs = e.currentTarget.dataset.hs;
        utils.uploadfile('', name, url, '', hs, function (res) {
            if(res.data.code==200){
                var data = res.data.data
                that.selectComponent("#loading").hideLoadingCus();
                wx.navigateTo({
                    url: '/pages/my/pdfsee/pdfsee?url=' + data.printUrl + '&name=' + data.localName
                })
            }else{
                that.selectComponent("#loading").hideLoadingCus();
                utils.toast(res.data.msg,2000)
            }
        })
    },
    // 确定打印
    chooseOk() {
        var that = this;
        if (utils.is_define(that.data.chooseIndex) || that.data.chooseIndex == '0') {
            var name = that.data.list[that.data.chooseIndex].sheetName;
            var url = that.data.list[that.data.chooseIndex].downUrl;
            var hs = that.data.list[that.data.chooseIndex].hs;
            var hsStr = hs == '1' ? '竖版' : '横版';
            wx.showModal({
                title: '打印确认',
                content: name + '该表格将以' + hsStr + '进行打印,请确认您已预览',
                showCancel: true,
                confirmText: '确认',
                success: function (res) {
                    if (res.confirm) {
                       that.selectComponent("#loading").showLoadingCus('加密上传中');
                        utils.uploadfile('', name, url, '', hs, function (res) {
                             if(res.data.code==200){
                                                             var data = res.data.data
                            that.selectComponent("#loading").hideLoadingCus();
                            if (utils.is_define(that.data.zip)) {
                                wx.setStorageSync('isZip', '1')
                                wx.redirectTo({
                                    url: '../list/list?code=' + that.data.code + '&name=' + data.localName + '&path=' + data.printUrl + '&pageNum=' + data.pageTotal + '&fileUrl=' + data.fileUrl + '&serviceMainId=' + that.data.serviceMainId + '&serviceSubtypeId=' + that.data.serviceSubtypeId + '&tagFloor=' + that.data.tagFloor+'&orientation='+hs
                                })
                            }
                            if (utils.is_define(that.data.isList)) {
                                var obj = {};
                                obj.pageNum = data.pageTotal
                                obj.taskOrderNo = '',
                                    obj.code = that.data.code;
                                obj.machineQrCode = wx.getStorageSync('machineQrCode'),
                                    obj.serviceMainId = that.data.serviceMainId,
                                    obj.serviceSubtypeId = that.data.serviceSubtypeId,
                                    obj.name = data.localName,
                                    obj.htFileUrl = data.fileUrl,
                                    obj.fileUrl = data.printUrl,
                                    obj.currentNum = 1,
                                    obj.orientation = hs,
                                    obj.currentbegin = 1,
                                    obj.currentend = data.pageTotal,
                                    obj.pageRange = '1-' + data.pageTotal,
                                    obj.currentFace = 1,
                                    obj.currentColor = 1,
                                    obj.currentPaper = 'A4',
                                    obj.margins = 1,
                                    obj.couponListId = "",
                                    obj.tagFloor = that.data.tagFloor
                                let pages = getCurrentPages();
                                let prevPage = pages[pages.length - 2];
                                prevPage.setData({
                                    currentObj: obj,
                                    without: 1,
                                    type: 2
                                })
                                wx.navigateBack()
                            } else if (utils.is_define(that.data.isCY)) {
                                wx.redirectTo({
                                    url: '../printer/printer?code=' + that.data.code + '&name=' + data.localName + '&path=' + data.printUrl + '&pageNum=' + data.pageTotal + '&fileUrl=' + data.fileUrl + '&serviceMainId=' + that.data.serviceMainId + '&serviceSubtypeId=' + that.data.serviceSubtypeId + '&tagFloor=' + that.data.tagFloor+'&orientation='+hs
                                })
                            } else {
                                                                wx.redirectTo({
                                    url: '../list/list?code=' + that.data.code + '&name=' + data.localName + '&path=' + data.printUrl + '&pageNum=' + data.pageTotal + '&fileUrl=' + data.fileUrl + '&serviceMainId=' + that.data.serviceMainId + '&serviceSubtypeId=' + that.data.serviceSubtypeId + '&tagFloor=' + that.data.tagFloor+'&orientation='+hs
                                })

                            }
                             }else{
                                  that.selectComponent("#loading").hideLoadingCus();
                                    utils.toast(res.data.msg,2000)
                             }

                        })
                    }
                }
            })

        } else {
            utils.toast('请先选择需要打印的sheet')
            return false;
        }

    },
    choosehs(e) {
        var index = e.currentTarget.dataset.index;
        var hs = e.currentTarget.dataset.hs;
        this.setData({
            ['list[' + index + '].hs']: hs
        })
    },
    chooseSheet(e) {
        var index = e.currentTarget.dataset.index;
        this.setData({
            chooseIndex: index
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this;
        var newList = JSON.parse(unescape(options.list));
        for (var i = 0; i < newList.length; i++) {
            newList[i].hs = 1;
        }
        that.setData({
            list: newList,
            serviceMainId: options.serviceMainId,
            code: options.code,
            serviceSubtypeId: options.serviceSubtypeId,
            tagFloor: options.tagFloor,
            isList: options.isList || '',
            isCY: options.isCY || '',
            zip: options.zip
        })
        wx.getSystemInfo({
            success: (result) => {
                var model = result.model;
                if (model == 'iPhone X') {
                    that.setData({
                        pdbottom: '68',
                    })
                } else {
                    that.setData({
                        pdbottom: '40'
                    })
                }
            },
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    }
})